package com.mobimagic.lockscreen.view;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mobimagic.lockscreen.Charge;
import com.mobimagic.lockscreen.R;
import com.mobimagic.lockscreen.util.NetworkUtils;

public class AvailableTimeView extends LinearLayout {

    private TextView currentNetWork, callAvailTime, netWorkAvailTime, movieAvailTime;
    private String callTime;
    private String movieTime;
    private String netTime;
    private int str;

    public AvailableTimeView(Context context) {
        super(context);
        View.inflate(context, R.layout.battery_avaliable_time_view, this);
        initView();
    }

    private void initView() {
        currentNetWork = (TextView) findViewById(R.id.network_type);
        callAvailTime = (TextView) findViewById(R.id.calling_time);
        netWorkAvailTime = (TextView) findViewById(R.id.network_time);
        movieAvailTime = (TextView) findViewById(R.id.movie_time);
    }

    public void initData(Charge charge) {
        int type = NetworkUtils.getNetWorkType(getContext());
        switch (type) {
        case NetworkUtils.NETWORK_CLASS_2_G:
            str = R.string.internet_on_2g;
            break;
        case NetworkUtils.NETWORK_CLASS_3_G:
            str = R.string.internet_on_3g;
            break;
        case NetworkUtils.NETWORK_CLASS_4_G:
            str = R.string.internet_on_4g;
            break;
        case NetworkUtils.NETWORK_CLASS_NO_NETWORK:
        case NetworkUtils.NETWORK_CLASS_UNKNOW:
        case NetworkUtils.NETWORK_CLASS_WIFI:
            str = R.string.internet_on_wifi;
            break;
        default:
            break;
        }
        if (charge == null) {
            return;
        }
        //电话
        float callHour = charge.getUseTimeForHour(Charge.UseTimeType.Call);
        callTime = String.format(getResources().getString(R.string.avai_times), getHour(callHour), getMinute(callHour));
        //上网
        float net = charge.getUseTimeForHour(Charge.UseTimeType.NetWork);
        netTime = String.format(getResources().getString(R.string.avai_times), getHour(net), getMinute(net));
        //看视频
        float movie = charge.getUseTimeForHour(Charge.UseTimeType.Movie);
        movieTime = String.format(getResources().getString(R.string.avai_times), getHour(movie), getMinute(movie));
        flushView();
    }

    private void flushView() {
        currentNetWork.setText(str);
        callAvailTime.setText(callTime);
        netWorkAvailTime.setText(netTime);
        movieAvailTime.setText(movieTime);
    }

    private int getHour(float value) {
        return (int) Math.floor(value);
    }

    private int getMinute(float value) {
        double xx = value - Math.floor(value);
        return (int) (xx * 60);

    }

}
